package com.aihna.mall.service.impl;

import com.aihna.common.core.utils.MapstructUtils;
import com.aihna.common.mybatis.core.page.TableDataInfo;
import com.aihna.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.aihna.mall.domain.bo.StoreUserBillBo;
import com.aihna.mall.domain.vo.StoreUserBillVo;
import com.aihna.mall.domain.StoreUserBill;
import com.aihna.mall.mapper.StoreUserBillMapper;
import com.aihna.mall.service.IStoreUserBillService;
import com.aihna.common.core.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户账单Service业务层处理
 *
 * @author Joseph.Chen
 * @date 2024-04-16
 */
@RequiredArgsConstructor
@Service
public class StoreUserBillServiceImpl implements IStoreUserBillService {

    private final StoreUserBillMapper baseMapper;

    /**
     * 查询用户账单
     */
    @Override
    public StoreUserBillVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户账单列表
     */
    @Override
    public TableDataInfo<StoreUserBillVo> queryPageList(StoreUserBillBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StoreUserBill> lqw = buildQueryWrapper(bo);
        Page<StoreUserBillVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户账单列表
     */
    @Override
    public List<StoreUserBillVo> queryList(StoreUserBillBo bo) {
        LambdaQueryWrapper<StoreUserBill> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StoreUserBill> buildQueryWrapper(StoreUserBillBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StoreUserBill> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUid() != null, StoreUserBill::getUid, bo.getUid());
        lqw.eq(bo.getPm() != null, StoreUserBill::getPm, bo.getPm());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), StoreUserBill::getCategory, bo.getCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), StoreUserBill::getType, bo.getType());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            StoreUserBill::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增用户账单
     */
    @Override
    public Boolean insertByBo(StoreUserBillBo bo) {
        StoreUserBill add = MapstructUtils.convert(bo, StoreUserBill.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户账单
     */
    @Override
    public Boolean updateByBo(StoreUserBillBo bo) {
        StoreUserBill update = MapstructUtils.convert(bo, StoreUserBill.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StoreUserBill entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户账单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
